;(function ($, window, document, undefined) {

    var share = function (ele, opt) {

        this.$element = ele;           //获取到的jQuery对象console.log(this);

        //设置默认参数
        this.defaults = {
            title: encodeURIComponent('我是分享标题'),	//分享标题
            link: encodeURIComponent('https://www.weijinzaixian.com/'),	//分享链接
            img: encodeURIComponent('https://www.weijinzaixian.com/static/activity/promote_ac/wap/imag_share.jpg'),	//分享图片
            des: '我是分享描述',	//默认分享描述
            "qq": {btn: '#qqBtn'},	// QQ
            "qzone": {btn: '#qZoneBtn'},	// QQ空间
            "weibo": {weibokey: 1252112689, btn: '#weiBoBtn'},	// 微博
            "wechat": {btn: "#weChatBtn", method: null}
        };

        this.options = $.extend({}, this.defaults, opt);

        //定义全局变量
        var _ = this;
        var shareBtn = ".shareBtn";

        // // 定义方法
        this.share_open = function () {
            _.share_weibo();
            _.share_qq();
            _.share_qzone();
        };

        // 微博
        this.share_weibo = function () {
            $(_.options.weibo.btn).click(function () {
                openUrl(
                    "weibo",
                    _.options.link,
                    _.options.title,
                    _.options.img,
                    "",
                    _.options.weibo.weibokey
                );
            })
        };
        // QQ
        this.share_qq = function () {
            $(_.options.qq.btn).click(function () {
                openUrl(
                    "qq",
                    _.options.link,
                    _.options.title,
                    _.options.img,
                    _.options.des,
                    ""
                );
            })
        };
        // QQ空间
        this.share_qzone = function () {
            $(_.options.qzone.btn).click(function () {
                openUrl(
                    "qzone",
                    _.options.link,
                    _.options.title,
                    _.options.img,
                    _.options.des,
                    ""
                );
            })
        };
        // 微信
        this.share_wechat = function () {
            $(_.options.wechat.btn).click(function () {
                if (typeof(_.options.wechat.method) === 'function') {
                    _.options.wechat.method();
                }
            })
        }
        // end
    };

    var openUrl = function (type, link, title, image, des, key) {
        var weiboUrl = "http://v.t.sina.com.cn/share/share.php?appkey=" + key + "&url=" + link + "&title=" + title + "&content=utf8&pic=" + image;
        var qqUrl = "http://connect.qq.com/widget/shareqq/index.html?url=" + link + "&title=" + title + "&content=utf8&summary=" + des + "&pics=" + image;
        var qZoneUrl = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + link + "&title=" + title + "&summary=" + des + "&pics=" + image;
        var url = (type === "weibo" ? weiboUrl : (type === "qq" ? qqUrl : qZoneUrl));
        window.open(url);

    };

    // 定义share的方法
    share.prototype = {
        init: function () {
            var _ = this;
            _.share_open();
        }
    };

    /**
     *
     * $.fn里面应专注于插件的调用
     *
     **/
    //在插件中使用share对象
    $.fn.share = function (options) {
        //创建Banner的实体
        var mShare = new share(this, options);
        //调用其方法
        return mShare.init();
    }

})(jQuery, window, document);
